package com.kye.express.business.jd.goods_list_by_category.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.base.utils.GsonUtils;
import com.kye.express.R;
import com.kye.express.business.jd.goods_list_by_category.model.JDCategorySelectModel;
import com.kye.express.databinding.ItemJdListByCategorySelectBinding;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 29/01/2018
 * @updated : 29/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class JDCategorySelectViewBinder extends ItemViewBinder<JDCategorySelectModel, JDCategorySelectViewBinder.ViewHolder> {

    private OnCategoryItemListener onCategoryItemListener;

    public JDCategorySelectViewBinder(OnCategoryItemListener onCategoryItemListener) {
        this.onCategoryItemListener = onCategoryItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_jd_list_by_category_select, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JDCategorySelectModel item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemJdListByCategorySelectBinding binding;
        private JDCategorySelectModel item;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.categoryName)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            /**
                             * todo : 更新选项，回调刷新数据
                             */
                            Logger.d("category selected : \n" + GsonUtils.toJsonString(item, true));
                            item.setSelected(true);
                            List<JDCategorySelectModel> items = (List<JDCategorySelectModel>) getAdapter().getItems();
                            Observable
                                    .just(item)
                                    .doOnNext(new Consumer<JDCategorySelectModel>() {
                                        @Override
                                        public void accept(JDCategorySelectModel jdCategorySelectModel) throws Exception {
                                            jdCategorySelectModel.setSelected(true);
                                        }
                                    })
                                    .flatMap(new Function<JDCategorySelectModel, Observable<JDCategorySelectModel>>() {
                                        @Override
                                        public Observable<JDCategorySelectModel> apply(JDCategorySelectModel jdCategorySelectModel) throws Exception {
                                            return Observable.fromIterable(items);
                                        }
                                    })
                                    .map(new Function<JDCategorySelectModel, JDCategorySelectModel>() {
                                        @Override
                                        public JDCategorySelectModel apply(JDCategorySelectModel jdCategorySelectModel) throws Exception {
                                            if (!jdCategorySelectModel.equals(item)) {
                                                jdCategorySelectModel.setSelected(false);
                                            } else {
                                                jdCategorySelectModel.setSelected(true);
                                            }
                                            return jdCategorySelectModel;
                                        }
                                    })
                                    .toList()
                                    .subscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer<List<JDCategorySelectModel>>() {
                                        @Override
                                        public void accept(List<JDCategorySelectModel> jdIntegralSelectModels) throws Exception {
                                            itemView.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    getAdapter().notifyDataSetChanged();
                                                    if (onCategoryItemListener != null) {
                                                        onCategoryItemListener.onItemClicked(item);
                                                    }
                                                }
                                            });
                                        }
                                    });
                        }
                    });
        }

        public void bindData(JDCategorySelectModel item) {
            this.item = item;
            binding.setData(item);
            int colorUnselected = itemView.getContext().getResources().getColor(R.color.black_666);
            int colorSelected = itemView.getContext().getResources().getColor(R.color.orange_FB793F);
            binding.categoryName.setTextColor(item.isSelected() ? colorSelected : colorUnselected);

        }
    }

    public interface OnCategoryItemListener {
        void onItemClicked(JDCategorySelectModel item);
    }
}
